package com.iogogogo.supervisord4j.util.parser;


import com.iogogogo.supervisord4j.util.Java8DateTimeUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Created by tao.zeng on 2022/1/18.
 */
public class UnixMillisEpochParser implements TimestampParser {

    private final static long MAX_EPOCH_MILLISECONDS = (long) Integer.MAX_VALUE * 1000;

    private final long value;

    public UnixMillisEpochParser(long value) {
        this.value = value;
    }

    @Override
    public LocalDateTime parse(List<DateTimeFormatter> pattern) {
        if (value > MAX_EPOCH_MILLISECONDS) {
            throw new IllegalArgumentException("Cannot parse date for value larger than UNIX epoch maximum seconds");
        }
        Instant instant = Java8DateTimeUtils.ofEpochMilli(value);
        return Java8DateTimeUtils.toJava8(instant);
    }
}
